
---@type SourceModifierList
local SourceModifierList = require("util/sourcemodifierlist")

---@class components
---@field ymh_kill_boss_getbuff component_ymh_kill_boss_getbuff

-- local function on_val(self, value)
    -- self.inst.replica.ymh_kill_boss_getbuff:SetVal(value)
-- end

---@class component_ymh_kill_boss_getbuff
---@field inst ent
---@field list table<PrefabID,boolean>
---@field buff SourceModifierList
local ymh_kill_boss_getbuff = Class(
---@param self component_ymh_kill_boss_getbuff
---@param inst ent
function(self, inst)
    self.inst = inst
    -- self.val = 0
    self.list = {}

    self.buff = SourceModifierList(self.inst,0,SourceModifierList.additive)

    inst:ListenForEvent('killed',
    ---comment
    ---@param _ ent
    ---@param data event_data_killed
    function (_,data)
        local victim = data and data.victim
        if victim and victim:HasTag('epic') and not victim:HasTag("structure") and not victim:HasTag("wall") then
            local victim_prefab = victim.prefab
            if victim_prefab and not self.list[victim_prefab] then
                self.list[victim_prefab] = true
                self.buff:SetModifier(self.inst,TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.DMGMULT_ADD_WHEN_KILL_BOSS,victim_prefab)
            end
        end
    end)

    self:_calcBuff()
end,
nil,
{
    -- val = on_val,
})

function ymh_kill_boss_getbuff:OnSave()
    local sav_data = {}

    sav_data.list = {}
    for k,v in pairs(self.list) do
        sav_data.list[k] = v
    end
    return sav_data
end

function ymh_kill_boss_getbuff:OnLoad(sav_data)
    -- self.val = data.val or 0
    for k,v in pairs(sav_data.list or {}) do
        self.list[k] = v
    end

    self:_calcBuff()
end


function ymh_kill_boss_getbuff:_calcBuff()
    for k,v in pairs(self.list) do
        self.buff:SetModifier(self.inst,TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.DMGMULT_ADD_WHEN_KILL_BOSS,k)
    end
end

---获取buff 伤害倍率
---@return number
---@nodiscard
function ymh_kill_boss_getbuff:getBuff()
    local val = self.buff:Get()
    ---@cast val number
    return val
end

return ymh_kill_boss_getbuff